Skip to content

fix: catch FormatException in log formatters to prevent third-party logger crashes#8070

Merged
Aaronontheweb merged 3 commits into
akkadotnet:devfrom
Aaronontheweb:fix/format-exception-crash-third-party-loggers
Mar 3, 2026
Merged

fix: catch FormatException in log formatters to prevent third-party logger crashes#8070
Aaronontheweb merged 3 commits into
akkadotnet:devfrom
Aaronontheweb:fix/format-exception-crash-third-party-loggers

Conversation

@Aaronontheweb
Copy link
Copy Markdown
Member

Summary

  • Catch FormatException in SemanticLogMessageFormatter and DefaultLogMessageFormatter positional format paths, returning a diagnostic string ([INVALID LOG FORMAT] str=[...], args=[...]) instead of throwing
  • Add defense-in-depth FormatException catches in LogFilterEvaluator.ShouldTryKeepMessage() to protect against custom formatters that may throw
  • Add 8 new unit tests covering both formatters and both evaluator paths

Problem

Bad format strings (e.g., "{0} {1} {2}" with only 2 args) caused FormatException to propagate through ShouldTryKeepMessage() and crash third-party logging actors (Serilog, NLog, etc.). The existing catch in StandardOutLogger.PrintLogEvent() only protected the console logger.

Stack trace from customer report:

System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
   at Akka.Event.SemanticLogMessageFormatter.Format(String format, IEnumerable`1 args)
   at Akka.Event.LogEvent.ToString()
   at Akka.Logger.Serilog.SerilogLogger.<.ctor>b__11_1(Warning w)

Test plan

  • SemanticLogMessageFormatter returns diagnostic for mismatched positional args (both object[] and IReadOnlyList paths)
  • DefaultLogMessageFormatter returns diagnostic for mismatched positional args
  • LogFilterEvaluator (empty and content-filter paths) handles FormatException gracefully
  • Valid format strings still format correctly (regression tests)
  • Existing LoggerSpec tests updated and passing

…-party logger crashes

Bad format strings (e.g., mismatched positional args) caused FormatException
to propagate through ShouldTryKeepMessage() and crash third-party logging
actors like SerilogLogger and NLogLogger. The existing catch in
StandardOutLogger.PrintLogEvent() only protected the console logger.

Now FormatException is caught at the formatter level in both
SemanticLogMessageFormatter and DefaultLogMessageFormatter, returning a
diagnostic string with the raw format and args. Defense-in-depth catches
are also added in LogFilterEvaluator.ShouldTryKeepMessage() to protect
against custom formatters that may throw.
@Aaronontheweb Aaronontheweb added this to the 1.5.62 milestone Mar 2, 2026
@Aaronontheweb Aaronontheweb enabled auto-merge (squash) March 2, 2026 19:21
Copy link
Copy Markdown
Contributor

@Arkatufus Arkatufus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Aaronontheweb Aaronontheweb disabled auto-merge March 3, 2026 15:37
@Aaronontheweb Aaronontheweb merged commit 0f35ad7 into akkadotnet:dev Mar 3, 2026
9 of 12 checks passed
@Aaronontheweb Aaronontheweb deleted the fix/format-exception-crash-third-party-loggers branch March 3, 2026 15:37
Arkatufus added a commit to Arkatufus/akka.net that referenced this pull request Mar 3, 2026
…-party logger crashes (akkadotnet#8070)

Bad format strings (e.g., mismatched positional args) caused FormatException
to propagate through ShouldTryKeepMessage() and crash third-party logging
actors like SerilogLogger and NLogLogger. The existing catch in
StandardOutLogger.PrintLogEvent() only protected the console logger.

Now FormatException is caught at the formatter level in both
SemanticLogMessageFormatter and DefaultLogMessageFormatter, returning a
diagnostic string with the raw format and args. Defense-in-depth catches
are also added in LogFilterEvaluator.ShouldTryKeepMessage() to protect
against custom formatters that may throw.

Co-authored-by: Gregorius Soedharmo <arkatufus@yahoo.com>
(cherry picked from commit 0f35ad7)
Aaronontheweb added a commit that referenced this pull request Mar 3, 2026
…-party logger crashes (#8070) (#8073)

Bad format strings (e.g., mismatched positional args) caused FormatException
to propagate through ShouldTryKeepMessage() and crash third-party logging
actors like SerilogLogger and NLogLogger. The existing catch in
StandardOutLogger.PrintLogEvent() only protected the console logger.

Now FormatException is caught at the formatter level in both
SemanticLogMessageFormatter and DefaultLogMessageFormatter, returning a
diagnostic string with the raw format and args. Defense-in-depth catches
are also added in LogFilterEvaluator.ShouldTryKeepMessage() to protect
against custom formatters that may throw.


(cherry picked from commit 0f35ad7)

Co-authored-by: Aaron Stannard <aaron@aaronstannard.com>
This was referenced May 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants